package com.jdk.learn.exam.gui;

import org.junit.Test;

import javax.swing.*;
import javax.swing.event.MenuKeyEvent;
import javax.swing.event.MenuKeyListener;
import java.awt.*;
import java.awt.event.*;
import java.io.File;

/**
 * Created by ricky
 * on 2018/3/2.
 * <p>
 * 组件相关示例练习
 */
public class ComponentDemo {

    /**
     * 组合框+列表
     */
    @Test
    public void JComboBoxAndJList() {

        JFrame frame = GUIUtils.getDefaultFrame("组合框");
        Container container = frame.getContentPane();
        //组合框
        JComboBox jComboBox = new JComboBox();
        jComboBox.addItem("手动组合1");
        jComboBox.addItem("手动组合2");

        Object one = jComboBox.getItemAt(1);//下标为1的元素
        System.out.println(one);
        int count = jComboBox.getItemCount();
        System.out.println("元素数量:" + count);
        // 在指定位置添加新元素
        jComboBox.insertItemAt("手动添加3", count);//下标从0开始

        jComboBox.setEditable(true);//设置可编辑

        System.out.println("选择元素下标:" + jComboBox.getSelectedIndex());


        JPanel p1 = new JPanel();
        p1.setBorder(GUIUtils.getDefaultTitleBorder("JComboBox1"));
        p1.add(jComboBox);
        container.setLayout(new GridLayout(5, 1));
        container.add(p1);

        Object[] items = {"组合1", "组合2"};
        JComboBox jComboBox2 = new JComboBox(items);//直接设置好元素

        JPanel p2 = new JPanel();
        p2.setBorder(GUIUtils.getDefaultTitleBorder("JComboBox2"));
        p2.add(jComboBox2);
        container.add(p2);

        //列表
        DefaultListModel<String> model = new DefaultListModel<String>();
        model.addElement("1");
        model.addElement("2");
        model.addElement("3");

        JList list = new JList(model);
        //等价于下面
        String[] listeql = {"1", "2", "3"};
        JList list2 = new JList(listeql);


        JScrollPane p3 = new JScrollPane(list, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        p3.setBorder(GUIUtils.getDefaultTitleBorder("List1"));
        container.add(p3);


        JPanel p4 = new JPanel();
        p4.setBorder(GUIUtils.getDefaultTitleBorder("addList"));
        JTextArea txa = new JTextArea(2, 10);
        txa.setBorder(BorderFactory.createLineBorder(Color.BLACK));//设置文本域边框
        JButton button = new JButton("添加");
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String text = txa.getText();
                model.addElement(text);
                txa.setText("");
            }
        });
        p4.add(txa);
        p4.add(button);
        container.add(p4);


        frame.pack();
        GUIUtils.Sleep();
    }

    /**
     * 文本组件
     */
    public void TextComponent() {
        JFrame frame = GUIUtils.getDefaultFrame("组合框");
        Container container = frame.getContentPane();

        //文本域
        JTextField textField = new JTextField();
        JTextField textField2 = new JTextField("请输入您的内容", 30);//创建默认30列，内容为：请输入您的内容
        textField2.setEditable(false);//不可编辑
        textField2.setBorder(GUIUtils.getDefaultLineBorder(Color.cyan));
        //密码框
        JPasswordField passwordField = new JPasswordField();
        //文本区
        JTextArea textArea = new JTextArea("默认内容", 4, 5);
        //给文本区追加滚动条
        JScrollPane jsp = new JScrollPane(textArea);
        textArea.append("追加的内容");
        textArea.setBorder(GUIUtils.getDefaultLineBorder(Color.cyan));
        GUIUtils.Sleep();
    }


    /**
     * 文本组件练习
     * 从上边输入框中输入内容，点击拷贝，拷贝到下边。
     * 清空按钮，清空第二个文本区
     */
    @Test
    public void TextComponentExr() {
        JFrame frame = GUIUtils.getDefaultFrame("文本框+按钮练习");
        Container container = frame.getContentPane();

        //设置两个文本区  第二个不可编辑
        JTextArea ta1 = new JTextArea("默认内容", 4, 5);
        ta1.setBorder(BorderFactory.createEtchedBorder());
        JScrollPane jsp1 = new JScrollPane(ta1, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);

        JTextArea ta2 = new JTextArea(4, 5);
        ta2.setEditable(false);
        ta2.setBorder(BorderFactory.createEtchedBorder());
        JScrollPane jsp2 = new JScrollPane(ta2, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);

        JButton copy = new JButton("复制");
        copy.addActionListener((e) -> {
            if (ta1.getSelectedText() != null) {
                ta2.append(ta1.getSelectedText() + "\n");
            } else {
                ta2.append(ta1.getText() + "\n");
            }
        });
        JButton cls = new JButton("清空");

        cls.addActionListener((e) -> {
            ta2.setText("");
        });

        JPanel p1 = new JPanel();
        p1.add(jsp1);
        p1.add(copy);
        container.add(p1, BorderLayout.CENTER);
        JPanel p2 = new JPanel();
        p2.add(jsp2);
        p2.add(cls);
        container.add(p2, BorderLayout.SOUTH);

        GUIUtils.Sleep();
    }


    /**
     * 菜单相关
     */
    @Test
    public void MenuComponment() {
        JFrame frame = GUIUtils.getDefaultFrame("菜单");
        Container container = frame.getContentPane();
        //菜单栏
        JMenuBar bar = new JMenuBar();
//        bar.setName("文件");
        JMenu file = new JMenu("文件", true);//可分离
        JMenuItem open = new JMenuItem("打开");
//        JMenuItem exit=new JMenuItem("退出", KeyEvent.VK_Q);//设置快捷ctrl+q
        JMenuItem exit = new JMenuItem("退出");
//        exit.setMnemonic(KeyEvent.VK_Q);
        //显示快捷键名称到菜单项上，并注册快捷键ctrl+Q ，可以不用上述快捷键  并且全局生效
        exit.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q, ActionEvent.CTRL_MASK));
        exit.addActionListener((e) -> {
            System.exit(1);
        });
        /**
         * 监听菜单键盘事件
         * 只在菜单打开有效
         * 全局无效(配合(exit.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q,ActionEvent.CTRL_MASK));),才有效)
         */
        exit.addMenuKeyListener(new MenuKeyListener() {
            @Override
            public void menuKeyTyped(MenuKeyEvent e) {

            }

            @Override
            public void menuKeyPressed(MenuKeyEvent e) {
                System.out.println(e.getKeyCode());
                if (e.getKeyCode() == KeyEvent.VK_Q) {
                    System.out.println("--------退出--------");
                }
            }

            @Override
            public void menuKeyReleased(MenuKeyEvent e) {

            }
        });

        file.add(open);
        file.addSeparator();
        file.add(exit);
        JMenu edit = new JMenu("编辑");
        bar.add(file);
        bar.add(edit);
        /*frame.addKeyListener(new KeyAdapter() {
            @Override
            public void keyReleased(KeyEvent e) {
                System.out.println(e.getKeyCode());
                //ctrl+q退出
                if (e.getKeyCode()==KeyEvent.VK_Q) {
                    System.out.println("--------退出--------");
                    System.exit(1);
                }
            }
        });*/
        frame.setJMenuBar(bar);

        GUIUtils.Sleep();
    }


    /**
     * 菜单示例2
     * 可以使用ctrl+q退出程序
     */
    @Test
    public void MenuDemo2() {
        JFrame frame = GUIUtils.getDefaultFrame("菜单示例2");
        Container container = frame.getContentPane();
        //菜单栏
        JMenuBar bar = new JMenuBar();

        //菜单
        JMenu file = new JMenu("文件");
        JMenu edit = new JMenu("编辑");
        JMenu help = new JMenu("帮助");

        //子菜单
        JMenuItem open = new JMenuItem("打开");

        open.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                int i=1000;
                //对话框
             /*   JDialog dialog=new JDialog();
                dialog.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);*/
//                 i = JOptionPane.showConfirmDialog(container, "消息内容？！", "我是标题", 1);//确认框
//                 i = JOptionPane.showInternalConfirmDialog(container, "消息内容？！", "我是标题", 1);//窗体内部，不脱离窗体的确认框

                System.out.println("用户操作："+i);//0是 1 否 2 取消 -1 是关闭了对话框窗口
                if (i==0) {
                    System.out.println("用户确认了,可以进行其他操作了哦");
                }

//                String input = JOptionPane.showInputDialog(container, "消息内容？！", "我是标题", 1);//输入对话框
//                System.out.println("用户输入："+input);// null 是点击了否 或者关闭了  "" 空或者有内容是确认了操作 即输入空字符串或者有值
//                JOptionPane.showMessageDialog(container,"提示信息","信息标题",JOptionPane.INFORMATION_MESSAGE);//信息提示
//                JOptionPane.showMessageDialog(container,"提示信息","信息标题",JOptionPane.ERROR_MESSAGE);//信息提示  JOptionPane.ERROR_MESSAGE(图标)

//                Object[] options={"按钮1","按钮2"};
//                i = JOptionPane.showOptionDialog(container, "提示信息", "信息标题",
//                        JOptionPane.DEFAULT_OPTION, JOptionPane.ERROR_MESSAGE,
//                        null, options, options[0]);
//                System.out.println("选项结果:"+i);//选项从0开始 往后累加

                //文件对话框
//                JFileChooser fc=new JFileChooser();
                JFileChooser fc=new JFileChooser("d:\\");//默认打开d盘目录
                i = fc.showOpenDialog(container);//显示文件对话框
                if (i==JFileChooser.APPROVE_OPTION) {
                    File file1=fc.getSelectedFile();
                    System.out.println("文件名:"+file1.getName());
                }
            }
        });
        file.add(open);
        file.addSeparator();//添加分割线
        JMenuItem exit = new JMenuItem("退出");
        exit.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q, ActionEvent.CTRL_MASK));
        exit.addActionListener((e) -> {
            System.exit(1);
        });
        file.add(exit);


        //编辑栏相关

        //多选
        JCheckBoxMenuItem multibox = new JCheckBoxMenuItem("多选1");//多选菜单
        JCheckBoxMenuItem multibox2 = new JCheckBoxMenuItem("多选2");//多选菜单
        ItemListener itemListener = new ItemListener() {
            @Override
            public void itemStateChanged(ItemEvent e) {
                System.out.println("选中状态:" + e.getStateChange() + ",选中:" + ItemEvent.SELECTED);
            }
        };
        multibox.addItemListener(itemListener);
        edit.add(multibox);
        edit.add(multibox2);

        edit.addSeparator();
        //单选
        JRadioButtonMenuItem radio = new JRadioButtonMenuItem("单选1");

        edit.add(radio);
        JRadioButtonMenuItem radio2 = new JRadioButtonMenuItem("单选2");
        edit.add(radio2);

        radio.addItemListener(itemListener);
        ButtonGroup group = new ButtonGroup();
        group.add(radio);
        group.add(radio2);

        //菜单追加到菜单栏上
        bar.add(file);
        bar.add(edit);
        bar.add(help);
        frame.setJMenuBar(bar);
        frame.pack();
        GUIUtils.Sleep();
    }
}
